package service;

import dao.DoctorDao;
import dao.DoctorDaoImpl;
import pojo.Department;
import pojo.Doctor;
import pojo.DoctorPage;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 陈金平
 * @Description
 * @create 2020/11/18 15:10
 */
public class DoctorServiceImpl implements DoctorService{
    private DoctorDao doctorDao = new DoctorDaoImpl();
    @Override
    public DoctorPage getAllDoctors(Map<String, Object> params) {
        Integer page = (Integer) params.get("page");
        Integer size = (Integer) params.get("size");
        Integer start =(page-1)*size;

        params.put("start",start);
        params.put("length",size);

        List<Doctor> doctors = doctorDao.selectAllDoctors(params);
        Integer count = doctorDao.selectDoctorsCount(params);

        Integer total = (count % size == 0)?count / size : count / size + 1;

        DoctorPage doctorPage = new DoctorPage();
        doctorPage.setDoctorList(doctors);
        doctorPage.setRoeCount(count);
        doctorPage.setPageCount(total);
        doctorPage.setPageNum(page);
        doctorPage.setPageSize(size);
        return doctorPage;
    }

    @Override
    public Integer addOneDoctor(Doctor doctor) {
        Integer doctorId = doctorDao.insertOneDoctor(doctor);
        if (doctorId==null){
            return 0;
        }
        return doctorId;
    }

    @Override
    public Integer setOneDoctor(Doctor doctor) {
        if (doctorDao.updateOneDoctor(doctor)<=0){
            return 0;
        }
        return 1;
    }

    @Override
    public Doctor getDoctorById(Integer id) {
        return doctorDao.selectDoctorById(id);
    }

    @Override
    public Integer delAllDoctors(Integer id) {
        return doctorDao.deleteAllDoctors(id);
    }
}
